__author__   = 'Don B. Stringham'
__version__  = '0.67'
__revision__ = '0.1'
__date__     = '1/31/2006 10:37:16 AM'
__credits__  = ''
__license__  = '''Copyright(c) 2005 Don B. Stringham
All rights reserved.  Unauthorized reproduction
of this software is prohibited and is in
violation of United States copyright laws.'''
__doc__      = '''Helper class for easier access to os applications or
commands.'''
####Imports--------------------------------------------------------------------
import os
import sys
####Classes--------------------------------------------------------------------
class Shell:
    if os.name == 'nt' or os.name == 'dos':
        def __getattr__(self, command):
            def __call(*args, **keywords):
                import sys  #Necessary because builtin overrides don't pick up global imports
                if command == 'stdout':
                    return sys.stdout
                if command == 'stderr':
                    return sys.stderr
                exe = command
                exeargs = [exe]
                if keywords:
                    for i in args.iteritems():
                        exeargs.extend(i)
                if args:
                    exeargs.extend(args[0])
                exeargs = [str(i) for i in exeargs]
                import os #Necessary because builtin overrides don't pick up global imports
                cwd = os.path.abspath(os.curdir)
                import subprocess
                retcode = subprocess.call(exeargs, shell=True)
                return retcode
            return __call
    elif os.name == 'posix':
        def __init__(self):
            self.prefix = '/bin'
            self.env = {}
            self.stdout = None
            self.stderr = None
            self.wait = False

        def __getattr__(self, command):
            def __call(*args, **keywords):
                if command == 'prefix':
                    return self.prefix
                if command == 'stdout':
                    return self.stdout
                if command == 'stderr':
                    return self.stderr
                if command.startswith('__'):
                    return None
                if self.prefix:
                    exe = '%s/%s' % (self.prefix, command)
                else:
                    exe = command
                import os, subprocess
                if os.path.exists(exe):
                    exeargs = [exe]
                    if keywords:
                        for i in args.iteritems(): exeargs.extend(i)
                    if args:
                        exeargs.extend(args)
                    exeargs = [str(i) for i in exeargs]
                    cwd = os.path.abspath(os.curdir)
                    p = subprocess.Popen(exeargs,
                                         bufsize=1,
                                         cwd=cwd,
                                         env=self.env,
                                         stdout=subprocess.PIPE,
                                         close_fds=False,
                                         universal_newlines=True)
                    if self.wait:
                        ret = p.wait()
                    else:
                        ret = p.returncode
                    result = None
                    if p.stdout:
                        self.stdout = p.stdout.read()
                    if p.stderr:
                        self.stderr = p.stderr.read()
                    return ret
                else:
                    raise Exception('No executable found at %s' % exe)
            return __call
####Global function------------------------------------------------------------
sys.modules[__name__] = Shell()
